package com.betty.cscams.service.impl;

import java.util.List;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.SysDeptMapper;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.SysDept;
import com.betty.cscams.entity.SysRole;
import com.betty.cscams.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.betty.cscams.entity.SysUserDept;
import com.betty.cscams.dao.SysUserDeptMapper;
import com.betty.cscams.service.SysUserDeptService;

/**
 * ---------------------------
 *  (SysUserDeptServiceImpl)         
 * ---------------------------
 * 时间：  2022-03-12 15:11:45
 * ---------------------------
 */
@Service
public class SysUserDeptServiceImpl implements SysUserDeptService {

	@Autowired
	private SysUserDeptMapper sysUserDeptMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Override
	public int save(SysUserDept record) {
		if(record.getId() == null || record.getId() == 0) {
			return sysUserDeptMapper.add(record);
		}
		return sysUserDeptMapper.update(record);
	}

	@Override
	public int delete(SysUserDept record) {
		return sysUserDeptMapper.delete(record.getId());
	}

	@Override
	public int delete(List<SysUserDept> records) {
		for(SysUserDept record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public SysUserDept findById(Long id) {
		return sysUserDeptMapper.findById(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		return MybatisPageHelper.findPage(pageRequest, sysUserDeptMapper);
	}

	@Override
	public List<SysDept> findDeptsByUser(Long userId) {
		SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
		if(SysConstants.ADMIN.equalsIgnoreCase(sysUser.getName())) {
			// 如果是超级管理员，返回全部
			return sysDeptMapper.findAll();
		}
		return sysDeptMapper.findDeptByUserId(userId);
	}
}
